//
//  HMHomeToolbar.swift
//  Weibo24
//
//  Created by apple on 17/3/30.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

//  首页自定义的toolBar视图
class HMHomeToolbar: UIView {
    //  转发
    var retweetButton: UIButton?
    //  评论
    var commentButton: UIButton?
    //  赞
    var unlikeButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
            let count = statusViewModel?.status?.reposts_count ?? 0
            let retweetCountStr = String(count > 0 ? "\(count)" : "转发")
            let commentCountStr = String(statusViewModel?.status?.comments_count ?? 0)
            let unlikeCountStr = String(statusViewModel?.status?.attitudes_count ?? 0)
//            "\(statusViewModel?.status?.reposts_count ?? 0)"
            if retweetCountStr == "0" {
                retweetButton?.setTitle("转发", for: .normal)
            } else {
                retweetButton?.setTitle(retweetCountStr, for: .normal)
            }
            if commentCountStr == "0" {
                commentButton?.setTitle("评论", for: .normal)
            } else {
                commentButton?.setTitle(commentCountStr, for: .normal)
            }
            if unlikeCountStr == "0" {
                unlikeButton?.setTitle("赞", for: .normal)
            } else {
                unlikeButton?.setTitle(unlikeCountStr, for: .normal)
            }
            
        }
    }
    
    //  添加控件设置约束
    fileprivate func setupUI() {
        //  转发
        retweetButton = addChildButton(imageName: "timeline_icon_retweet", title: "转发")
        //  评论
        commentButton = addChildButton(imageName: "timeline_icon_comment", title: "评论")
        //  赞
        unlikeButton = addChildButton(imageName: "timeline_icon_unlike", title: "赞")
        
        retweetButton!.snp.makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp.makeConstraints { (make) in
            make.leading.equalTo(retweetButton!.snp.trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton!.snp.makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp.trailing)
        }
        //  创建两条竖线
        let firstLineView = addChildLineView()
        let secondLineView = addChildLineView()
        
        firstLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(retweetButton!.snp.trailing)
            make.centerY.equalTo(self)
        }
        
        secondLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(commentButton!.snp.trailing)
            make.centerY.equalTo(self)
        }
    }
    
    //  添加子按钮
    fileprivate func addChildButton(imageName: String, title: String) -> UIButton {
        let button = UIButton()
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        //  添加子视图
        addSubview(button)
        return button
    }
    
    //  添加子线视图
    fileprivate func addChildLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        //  添加图片
        addSubview(imageView)
        return imageView
    }
  

}
